package com.huawei.updatesdk.a.a.c.a;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.tencent.mm.opensdk.constants.ConstantsAPI.AppSupportContentFlag;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class a {
    private static Map<String, Object> a = new HashMap();
    private static a b = new a();
    private int c = 0;
    private int d = 0;

    private a() {
        int a = a("ro.maple.enable", 0);
        int a2 = a("persist.mygote.disable", 0);
        if (a == 1 && a2 != 1) {
            this.c = 1;
        }
        this.d = a("ro.build.version.ark", 0);
    }

    public static int a(String str, int i) {
        try {
            i = ((Integer) Class.forName("android.os.SystemProperties").getMethod("getInt", new Class[]{String.class, Integer.TYPE}).invoke(null, new Object[]{str, Integer.valueOf(i)})).intValue();
        } catch (Throwable e) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e);
        } catch (Throwable e2) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e2);
        } catch (Throwable e22) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e22);
        } catch (Throwable e222) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e222);
        } catch (Throwable e2222) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e2222);
        }
        return i;
    }

    public static String a() {
        return Build.DISPLAY;
    }

    public static String a(String str) {
        String str2;
        ClassNotFoundException e;
        NoSuchMethodException e2;
        NoSuchMethodError e3;
        Exception e4;
        Throwable th;
        try {
            Object invoke = Class.forName("com.huawei.hms.framework.network.grs.GrsApi").getMethod("synGetGrsUrl", new Class[]{String.class, String.class}).invoke(null, new Object[]{"com.huawei.update", DNKeeperConfig.SERVICE_KEY});
            if (!(invoke instanceof String)) {
                return str;
            }
            str2 = (String) invoke;
            try {
                Log.i("DeviceUtil", "UpdateSDK Get url from GRS_SDK Success!");
                return str2;
            } catch (ClassNotFoundException e5) {
                e = e5;
            } catch (NoSuchMethodException e6) {
                e2 = e6;
                Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e2.toString());
                return str2;
            } catch (NoSuchMethodError e7) {
                e3 = e7;
                Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e3.toString());
                return str2;
            } catch (Exception e8) {
                e4 = e8;
                Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e4.toString());
                return str2;
            } catch (Throwable th2) {
                th = th2;
                Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + th.toString());
                return str2;
            }
        } catch (ClassNotFoundException e9) {
            e = e9;
            str2 = str;
            Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e.toString());
            return str2;
        } catch (NoSuchMethodException e10) {
            e2 = e10;
            str2 = str;
            Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e2.toString());
            return str2;
        } catch (NoSuchMethodError e11) {
            e3 = e11;
            str2 = str;
            Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e3.toString());
            return str2;
        } catch (Exception e12) {
            e4 = e12;
            str2 = str;
            Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + e4.toString());
            return str2;
        } catch (Throwable th3) {
            th = th3;
            str2 = str;
            Log.w("DeviceUtil", "UpdateSDK Get url from GRS_SDK error: " + th.toString());
            return str2;
        }
    }

    public static String a(String str, String str2) {
        try {
            return (String) Class.forName("android.os.SystemProperties").getMethod("get", new Class[]{String.class, String.class}).invoke(null, new Object[]{str, str2});
        } catch (Throwable e) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e);
        } catch (Throwable e2) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e2);
        } catch (Throwable e22) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e22);
        } catch (Throwable e222) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e222);
        } catch (Throwable e2222) {
            com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "Exception while getting system property: ", e2222);
        }
        return str2;
    }

    public static void a(Context context) {
        b(context);
    }

    public static long b(Context context) {
        long longValue;
        Object obj = a.get("TotalMem");
        Long l = obj instanceof Long ? (Long) obj : null;
        if (l != null) {
            longValue = l.longValue();
        } else {
            longValue = VERSION.SDK_INT >= 16 ? i(context) : j(context);
            if (longValue > 0) {
                a.put("TotalMem", Long.valueOf(longValue));
            }
        }
        com.huawei.updatesdk.a.a.b.a.a.a.a("DeviceUtil", "getTotalMem, totalMem:" + longValue);
        return longValue;
    }

    public static String b() {
        Object obj;
        String str = "en";
        String str2 = "";
        String str3 = "US";
        Locale locale = Locale.getDefault();
        if (locale != null) {
            str = locale.getLanguage();
            if (VERSION.SDK_INT >= 21) {
                str2 = locale.getScript();
            }
            obj = str2;
            str2 = locale.getCountry();
        } else {
            String str4 = str3;
            str3 = str2;
            str2 = str4;
        }
        return TextUtils.isEmpty(obj) ? str + "_" + str2 : str + "_" + obj + "_" + str2;
    }

    public static int c() {
        try {
            Class cls = Class.forName("android.os.UserHandle");
            Object invoke = cls.getMethod("myUserId", new Class[0]).invoke(cls, new Object[0]);
            return invoke instanceof Integer ? ((Integer) invoke).intValue() : 0;
        } catch (IllegalArgumentException e) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid IllegalArgumentException! " + e.toString());
            return 0;
        } catch (ClassNotFoundException e2) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid ClassNotFoundException! " + e2.toString());
            return 0;
        } catch (NoSuchMethodException e3) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid NoSuchMethodException! " + e3.toString());
            return 0;
        } catch (InvocationTargetException e4) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid InvocationTargetException! " + e4.toString());
            return 0;
        } catch (IllegalAccessException e5) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid IllegalAccessException! " + e5.toString());
            return 0;
        } catch (Exception e6) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid IllegalAccessException! " + e6.toString());
            return 0;
        } catch (Throwable th) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", "get current uid IllegalAccessException! " + th.toString());
            return 0;
        }
    }

    public static String c(Context context) {
        String str = "";
        DisplayMetrics k = k(context);
        return k != null ? String.valueOf(k.densityDpi) : str;
    }

    public static String d() {
        return VERSION.RELEASE.trim();
    }

    public static String d(Context context) {
        String str = "";
        DisplayMetrics k = k(context);
        if (k == null) {
            return str;
        }
        str = String.valueOf(k.widthPixels);
        return str + "_" + String.valueOf(k.heightPixels);
    }

    public static a e() {
        return b;
    }

    public static boolean e(Context context) {
        PackageManager packageManager = context.getPackageManager();
        try {
            packageManager.getPackageInfo("com.google.android.gsf.login", 16);
            try {
                packageManager.getPackageInfo("com.google.android.gsf", 16);
                try {
                    return (packageManager.getPackageInfo("com.google.android.gms", 16).applicationInfo.flags & 1) != 0;
                } catch (Exception e) {
                    com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e.getMessage());
                    return false;
                }
            } catch (NameNotFoundException e2) {
                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e2.getMessage());
                return false;
            }
        } catch (NameNotFoundException e22) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e22.getMessage());
            return false;
        }
    }

    public static int f(Context context) {
        int i = 1;
        try {
            i = Integer.parseInt(context.getPackageManager().getPackageInfo(context.getPackageName(), 16).versionCode + "");
        } catch (NameNotFoundException e) {
        } catch (NumberFormatException e2) {
        }
        return i;
    }

    public static String g(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 16).versionName + "";
        } catch (NameNotFoundException e) {
            return null;
        }
    }

    public static String h(Context context) {
        String g = g(context);
        if (g != null) {
            int i = 0;
            int i2 = 0;
            while (i < g.length() && i2 < 3) {
                if (g.charAt(i) == '.') {
                    i2++;
                }
                i++;
            }
            if (3 == i2) {
                return g.substring(0, i - 1);
            }
        }
        return g;
    }

    @SuppressLint({"NewApi"})
    private static long i(Context context) {
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService("activity");
            MemoryInfo memoryInfo = new MemoryInfo();
            activityManager.getMemoryInfo(memoryInfo);
            return memoryInfo.totalMem;
        } catch (Exception e) {
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e.getMessage());
            return 0;
        }
    }

    private static long j(Context context) {
        FileInputStream fileInputStream;
        IOException e;
        NumberFormatException e2;
        Throwable th;
        UnsupportedEncodingException e3;
        FileNotFoundException e4;
        BufferedReader bufferedReader = null;
        long j = 0;
        try {
            fileInputStream = new FileInputStream("/proc/meminfo");
            try {
                BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
                try {
                    String readLine = bufferedReader2.readLine();
                    if (readLine != null) {
                        j = Long.parseLong(readLine.split("\\s+")[1]);
                    }
                    if (bufferedReader2 != null) {
                        try {
                            bufferedReader2.close();
                        } catch (IOException e5) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5.getMessage());
                        }
                    }
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e52) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e52.getMessage());
                        }
                    }
                } catch (NumberFormatException e6) {
                    e2 = e6;
                    bufferedReader = bufferedReader2;
                    try {
                        com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e2.getMessage());
                        if (bufferedReader != null) {
                            try {
                                bufferedReader.close();
                            } catch (IOException e522) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e522.getMessage());
                            }
                        }
                        if (fileInputStream != null) {
                            try {
                                fileInputStream.close();
                            } catch (IOException e5222) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5222.getMessage());
                            }
                        }
                        return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
                    } catch (Throwable th2) {
                        th = th2;
                        if (bufferedReader != null) {
                            try {
                                bufferedReader.close();
                            } catch (IOException e7) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e7.getMessage());
                            }
                        }
                        if (fileInputStream != null) {
                            try {
                                fileInputStream.close();
                            } catch (IOException e72) {
                                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e72.getMessage());
                            }
                        }
                        throw th;
                    }
                } catch (UnsupportedEncodingException e8) {
                    e3 = e8;
                    bufferedReader = bufferedReader2;
                    com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e3.getMessage());
                    if (bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e52222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e52222.getMessage());
                        }
                    }
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e522222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e522222.getMessage());
                        }
                    }
                    return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
                } catch (FileNotFoundException e9) {
                    e4 = e9;
                    bufferedReader = bufferedReader2;
                    com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e4.getMessage());
                    if (bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e5222222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5222222.getMessage());
                        }
                    }
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e52222222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e52222222.getMessage());
                        }
                    }
                    return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
                } catch (IOException e10) {
                    e52222222 = e10;
                    bufferedReader = bufferedReader2;
                    com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e52222222.getMessage());
                    if (bufferedReader != null) {
                        try {
                            bufferedReader.close();
                        } catch (IOException e522222222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e522222222.getMessage());
                        }
                    }
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e5222222222) {
                            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5222222222.getMessage());
                        }
                    }
                    return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
                } catch (Throwable th3) {
                    th = th3;
                    bufferedReader = bufferedReader2;
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                    throw th;
                }
            } catch (NumberFormatException e11) {
                e2 = e11;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e2.getMessage());
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
            } catch (UnsupportedEncodingException e12) {
                e3 = e12;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e3.getMessage());
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
            } catch (FileNotFoundException e13) {
                e4 = e13;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e4.getMessage());
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
            } catch (IOException e14) {
                e5222222222 = e14;
                com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5222222222.getMessage());
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
            }
        } catch (NumberFormatException e15) {
            e2 = e15;
            fileInputStream = null;
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e2.getMessage());
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
        } catch (UnsupportedEncodingException e16) {
            e3 = e16;
            fileInputStream = null;
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e3.getMessage());
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
        } catch (FileNotFoundException e17) {
            e4 = e17;
            fileInputStream = null;
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e4.getMessage());
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
        } catch (IOException e18) {
            e5222222222 = e18;
            fileInputStream = null;
            com.huawei.updatesdk.a.a.b.a.a.a.d("DeviceUtil", e5222222222.getMessage());
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
        } catch (Throwable th4) {
            th = th4;
            fileInputStream = null;
            if (bufferedReader != null) {
                bufferedReader.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            throw th;
        }
        return j * AppSupportContentFlag.MMAPP_SUPPORT_XLS;
    }

    private static DisplayMetrics k(Context context) {
        Object systemService = context.getSystemService("window");
        WindowManager windowManager = systemService instanceof WindowManager ? (WindowManager) systemService : null;
        if (windowManager != null) {
            Display defaultDisplay = windowManager.getDefaultDisplay();
            if (defaultDisplay != null) {
                DisplayMetrics displayMetrics = new DisplayMetrics();
                defaultDisplay.getMetrics(displayMetrics);
                return displayMetrics;
            }
        }
        return null;
    }

    public int f() {
        return this.c;
    }

    public int g() {
        return this.d;
    }
}
